package com.sykj.qianxuezhan.controller;

import com.alibaba.fastjson.JSONArray;
import com.baidu.aip.ocr.AipOcr;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sykj.config.oss.FileUploadUtil;
import com.sykj.qianxuezhan.bean.BagProject;
import com.sykj.qianxuezhan.bean.NurseBag;
import com.sykj.qianxuezhan.bean.ShopUser;
import com.sykj.qianxuezhan.server.BagProjectService;
import com.sykj.qianxuezhan.server.NurseBagService;
import com.sykj.qianxuezhan.server.NursePeojectService;
import com.sykj.qianxuezhan.utils.AIP;
import com.sykj.qianxuezhan.utils.JSONChange;
import com.sykj.util.file.FileUploadResult;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author QianXuezhan
 * @Date 2020/8/6 8:55
 * @Description: TODO
 */
@RestController
@CrossOrigin
@RequestMapping("/nurseBag")
public class NurseBagController {
    @Autowired
    private NurseBagService nurseBagService;
    @Autowired
    private NursePeojectService nursePeojectService;
    @Autowired
    private BagProjectService bagProjectService;

    private  FileUploadUtil fileUploadUtil;

    /**
     * @Description: 查询所有综合查询
     **/
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public List<NurseBag> queryAll(String bagId, String bagName, String status) {
        QueryWrapper q = new QueryWrapper();
        q.eq("is_del", "0");
        if (bagId != null && bagId != "") {
            q.like("bag_id", bagId);
        }
        if (bagName != null && bagName != "") {
            q.like("bag_name", bagName);
        }
        if (status != null && status != "") {
            q.eq("status", status);
        }
        return nurseBagService.list(q);
    }

    /**
     * @Description: 假删除--根据id修改是否删除--状态
     **/
    @RequestMapping("/updateDelId")
    public ResultBody updateDelId(String bagId) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bag_id", bagId);
        updateWrapper.set("is_del", "1");
        nurseBagService.update(updateWrapper);
        return new ResultBody(CommonEnum.SUCCESS);
    }

    //根据id查询护理项目表的信息
    @RequestMapping(value = "/queryProjectId", method = RequestMethod.GET)
    public Map queryProjectId(String projectId) {
        QueryWrapper q = new QueryWrapper();
        q.eq("project_id", projectId);
        q.select("project_id", "project_name", "type_id", "cover_photo", "cover_big_photo", "use_device", "server_time", "rmb_price", "beauty_price", "manual_price", "active_price", "status", "sort_no", "content", "is_del", "create_time", "create_user_id", "modify_time", "modify_user_id");
        Map map = new HashMap();
        map.put("st", nursePeojectService.getOne(q));
        return map;
    }

    /**
     * @Description: 添加礼包信息
     **/
    @RequestMapping("/addNurseBog")
    public ResultBody addNurseBog(String bagName, String status, Double bagTotalPrice, long openCardNnum, long minBuyNum, long maxBuyNum, long buyCount, int overdueTime, long sortNo, String bagIntroduce, String content, String jsonStr) {
        //将传过来的json转化为list
        //根据定时器生成id
        System.out.println(content);
        System.out.println(jsonStr);

        String bagId = Long.toString(System.currentTimeMillis());
        NurseBag nurseBag = new NurseBag();
        List<Map<String, Object>> matchRaws = JSONArray.parseObject(jsonStr, List.class);
        add(jsonStr, bagId);
        nurseBag.setBagId(bagId);
        nurseBag.setBagName(bagName);
        nurseBag.setStatus(status);
        nurseBag.setBagTotalPrice(bagTotalPrice);
        nurseBag.setOpenCardNum(openCardNnum);
        nurseBag.setMinBuyNum(minBuyNum);
        nurseBag.setMaxBuyNum(maxBuyNum);
        nurseBag.setBuyCount(buyCount);
        nurseBag.setOverdueTime(overdueTime);
        nurseBag.setSortNo(sortNo);
        nurseBag.setBagIntroduce(bagIntroduce);
        nurseBag.setContent(content);
        nurseBag.setCoverPhoto("http://oos-all.oss-cn-shenzhen.aliyuncs.com/qiao/854997.jpg");
        nurseBagService.addNurseBag(nurseBag);
        return new ResultBody(CommonEnum.SUCCESS);
    }

    /**
     * @Description: 根据id查询礼包信息
     **/
    @RequestMapping(value = "/queryNurseBagId", method = RequestMethod.GET)
    public Map queryNurseBagId(String bagId) {
        QueryWrapper q = new QueryWrapper();
        q.eq("bag_id", bagId);
        NurseBag nurseBag = nurseBagService.getOne(q);
        List list = bagProjectService.queryBagProId(bagId);
        Map map = new HashMap();
        map.put("nurseBag", nurseBag);
        map.put("bags", list);
        return map;
    }

    /**
     * @Description: 修改礼包信息
     **/
    @RequestMapping("/updateBag")
    public ResultBody updateBag(String bagId, String bagName, String status, Double bagTotalPrice, long openCardNnum, long minBuyNum, long maxBuyNum, long buyCount, long overdueTime, long sortNo, String bagIntroduce, String content, String jsonStr) {
        UpdateWrapper up = new UpdateWrapper();
        up.eq("bag_id", bagId);
        up.set("bag_name", bagName);
        up.set("status", status);
        up.set("bag_total_price", bagTotalPrice);
        up.set("open_card_num", openCardNnum);
        up.set("min_buy_num", minBuyNum);
        up.set("max_buy_num", maxBuyNum);
        up.set("buy_count", buyCount);
        up.set("overdue_time", overdueTime);
        up.set("sort_no", sortNo);
        up.set("bag_introduce", bagIntroduce);
        up.set("content", content);
        up.set("cover_photo","http://oos-all.oss-cn-shenzhen.aliyuncs.com/qiao/854997.jpg");
        nurseBagService.update(up);
        UpdateWrapper up2 = new UpdateWrapper();
        up2.eq("bag_id", bagId);
        bagProjectService.remove(up2);
        if (!jsonStr.equals("")) {
            add(jsonStr, bagId);
        }


        return new ResultBody(CommonEnum.SUCCESS);
    }

    /**
     * @Description: 后台传入数组--关联表添加
     **/
    public void add(String jsonStr, String bagId) {
        List<Map<String, Object>> matchRaws = JSONArray.parseObject(jsonStr, List.class);
        for (Map<String, Object> map : matchRaws) {
            BagProject bp = new BagProject();
            bp.setBagId(bagId);
            bp.setProjectId((String) map.get("projectId"));
            if (map.get("rmbPrice") != "" && map.get("rmbPrice") != null) {
                double a = Double.valueOf((String) map.get("rmbPrice")).doubleValue();
                bp.setProjectOldPrice(a);
                bp.setProjectNewPrice(a);
            } else {
                bp.setProjectOldPrice(0);
                bp.setProjectNewPrice(0);
            }
            if (map.get("number") != "undefined" && map.get("number") != null && map.get("number") != "" && !"".equals(map.get("number"))) {
                long l = Long.parseLong((String) map.get("number"));
                bp.setProjectNum(l);
            } else {
                bp.setProjectNum(0);
            }
            bagProjectService.addBagProject(bp);
        }
    }




}
